<template>
  <div
    id="editorViewer"
    ref="editorViewer"
    style="height: 99%; width: 100%; position: relative"
    v-show="status.isShow"
  >
    <div id="container" ref="container" class="content-container"></div>

    <div class="toolbar">
      <el-button-group>
        <!-- <el-tooltip effect="dark" content="Select" placement="top">
          <el-button @click="methods.drawTool('select')"
            ><SvgIcon icon-class="cursor"></SvgIcon>
          </el-button>
        </el-tooltip> -->
        <el-tooltip effect="dark" content="Mediate" placement="top">
          <el-button @click="methods.reset"><SvgIcon icon-class="mediate"></SvgIcon> </el-button>
        </el-tooltip>
        <el-tooltip effect="dark" content="Draw Back" placement="top">
          <el-button @click="methods.drawBack"><SvgIcon icon-class="back"></SvgIcon> </el-button>
        </el-tooltip>
        <el-tooltip effect="dark" content="eraser" placement="top">
          <el-button @click="methods.drawTool('remove')"
            ><SvgIcon icon-class="earser"></SvgIcon>
          </el-button>
        </el-tooltip>
        <el-tooltip effect="dark" content="Clean Up" placement="top">
          <el-button @click="methods.cleanUpAll">
            <SvgIcon icon-class="cleanup"></SvgIcon>
          </el-button>
        </el-tooltip>
      </el-button-group>
      <el-divider direction="vertical"></el-divider>
      <el-button-group>
        <el-tooltip effect="dark" content="Draw Fence" placement="top">
          <el-button @click="methods.drawTool('drawFence')"
            ><SvgIcon icon-class="fence"></SvgIcon>
          </el-button>
        </el-tooltip>
        <el-tooltip effect="dark" content="Draw Fences" placement="top">
          <el-button @click="methods.drawTool('drawFences')">
            <SvgIcon icon-class="fences"></SvgIcon>
          </el-button>
        </el-tooltip>
        <el-tooltip effect="dark" content="Draw Door" placement="top">
          <el-button @click="methods.drawTool('drawDoor')"
            ><SvgIcon icon-class="door"></SvgIcon>
          </el-button>
        </el-tooltip>
        <el-tooltip effect="dark" content="Add Text" placement="top">
          <el-button @click="methods.drawTool('drawText')"
            ><SvgIcon icon-class="text"></SvgIcon>
          </el-button>
        </el-tooltip>
      </el-button-group>

      <div class="mouseBar">
        <SvgIcon icon-class="pen" size="16"></SvgIcon>
        <SvgIcon icon-class="mouseLeft"></SvgIcon>
        <el-divider direction="vertical"></el-divider>
        <SvgIcon icon-class="zoomIn" size="16"></SvgIcon>
        <SvgIcon icon-class="mouseWheel"></SvgIcon>
        <el-divider direction="vertical"></el-divider>
        <SvgIcon icon-class="move" size="16"></SvgIcon>
        <SvgIcon icon-class="mouseRight"></SvgIcon>
      </div>
    </div>

    <SubPanelTool
      class="subPanel"
      ref="subPanelTool"
      id="subPanelTool"
      @upload-image="methods.setBackground"
      @background-rotate="methods.backgroundRotate"
      @finish-background="methods.finishBackground"
    />

    <!-- <el-dialog v-model="status.dialogPlottingScale" title="更新比例尺" width="30%" center>
      <div class="mouseBar">
        <el-input
          v-model="status.plottingScaleValue"
          size="mini"
          placeholder="0"
          style="width: 60%"
        />
        <span style="margin: 8px">米</span>
      </div>
      <template #footer>
        <span class="mouseBar">
          <el-button @click="status.dialogPlottingScale = false">Cancel</el-button>
          <el-button type="primary" @click="methods.finishBackground">Confirm</el-button>
        </span>
      </template>
    </el-dialog> -->
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, onMounted, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { Panel, CURSOR_ENUM } from '@/components/konvaUnits/panel'
import { useStore } from '@/store/index'
import SubPanelTool from '@/views/vichdraw/subPanelTool.vue'

export default defineComponent({
  components: { SubPanelTool },

  setup() {
    const store = useStore()
    const { t } = useI18n()
    const container = ref(null)
    const subPanelTool = ref(null)

    const status = reactive({
      isShow: true,
      subPanelTool: true,
      dialogPlottingScale: false,
      plottingScaleValue: 0
    })

    let panel: Panel | null = null
    const methods = {
      createContainer: async () => {
        if (panel === null) panel = new Panel((container as any).value)
      },
      select: (e: object) => {
        const { id } = e.attrs
        if (id.indexOf('fenceGroup') > -1) {
          status.subPanelTool = true
          status.fenceTool = true
          status.doorTool = false
        } else if (id.indexOf('doorGroup') > -1) {
          status.subPanelTool = true
          status.fenceTool = false
          status.doorTool = true
        }
        // panel?.tools(CURSOR_ENUM.SELECT_ENUM)
      },
      reset: () => {
        panel?.reset()
      },
      startDraw: () => {
        methods.createContainer()
        panel?.startDraw(methods.select)
        subPanelTool.value.status.tabName = 'fence'
      },
      showSubPanelTool: () => {
        subPanelTool.value.status.isShow = true
      },
      drawTool: (action: string) => {
        // subPanelTool.value.status.isShow = false
        panel?.tool(action)
      },
      backgroundRotate: () => {
        panel?.backgroundRotate()
      },

      setBackground: (file: string) => {
        methods.createContainer()

        panel?.setBackground(file)
        panel?.tool(CURSOR_ENUM.RULE_ENUM)
      },
      finishBackground: (value: string) => {
        panel?.updatePlottingScale(parseInt(value, 10))
        panel?.tool('drawFence')
        subPanelTool.value.status.tabName = 'fence'
      },
      drawBack: () => {
        panel?.drawBack()
      },
      cleanUpAll: () => {
        panel?.cleanUpAll()
      },
      getImageData: () => {
        return panel?.getImageData()
      }
    }
    onMounted(async () => {
      methods.startDraw()
    })
    return {
      container,
      subPanelTool,
      status,
      methods
    }
  }
})
</script>

<style scoped>
.content-container {
  height: 100%;
  width: 100%;
  position: absolute;
  background-color: #f0f0f0;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
  display: flex;
  justify-content: end;
}
.toolbar {
  width: 100%;
  margin: 8px;
  bottom: 10px;
  text-align: center;
  position: absolute;
  line-height: 60px;
}
.mouseBar {
  width: auto;
  text-align: center;
  line-height: 20px;
}
.subPanel {
  width: auto;
  margin: 8px;
  left: 8px;
  top: 8px;
  position: absolute;
}
</style>
